/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: GanttThread.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.rcp.ui.internal.thread;

import org.eclipse.swt.widgets.Display;
import org.foliage.rcp.ui.preference.DefaultGanttThreadHandler;
import org.foliage.rcp.ui.preference.IGanttThreadHandler;
import org.foliage.rcp.ui.widgets.Gantt;

public class GanttThread extends Thread {

	// ----------------------------------------------- 

	private Gantt gantt;

	private Runnable runnable;
	/** 甘特图线程适配器。 */
	private IGanttThreadHandler ganttThreadHandler = null;
	
	// ----------------------------------------------- 

	private class GanttRunnable implements Runnable {

		@Override
		public void run() {
			if (gantt != null && !gantt.isDisposed()) {
				ganttThreadHandler.onSyncThread(gantt);
			}
		}

	}

	// ----------------------------------------------- 

	public GanttThread(Gantt gantt) {
		this.gantt = gantt;
		this.ganttThreadHandler = new DefaultGanttThreadHandler();
		this.runnable = new GanttRunnable();
	}

	// ----------------------------------------------- 

	@Override
	public void run() {
		while (true) {
			if (gantt != null && !gantt.isDisposed()) {
				Display.getDefault().asyncExec(runnable);
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} else {
				return;
			}
		}
	}

}
